from datetime import datetime
from typing import List, Union, Literal

from sqlmodel import SQLModel, Field
from sqlalchemy import Column, Text

from app.models.base import EmptyStrToNone, PageParams


class SystemRole(SQLModel, table=True):
    __tablename__ = "system_role"

    id: int = Field(default=None, primary_key=True, index=True, nullable=False)
    name: str = Field(default='', max_length=100, nullable=False)
    remark: str = Field(default='', max_length=200, nullable=False)
    sort: int = Field(default=0, nullable=False)
    is_disable: int = Field(default=0, nullable=False)
    create_time: datetime = Field(default_factory=datetime.utcnow, nullable=False)
    update_time: datetime = Field(default_factory=datetime.utcnow, sa_column_kwargs={'onupdate': datetime.utcnow},
                                  nullable=False)


class SystemRoleAddIn(SQLModel):
    """新增角色参数"""
    name: str = Field(min_length=1, max_length=30)  # 角色名称
    sort: int = Field(ge=0)  # 角色排序
    remark: str = Field(default='', max_length=200)  # 角色备注
    is_disable: int = Field( ge=0, le=1)  # 是否禁用: [0=否, 1=是]
    menu_id: Union[list, None]  # 关联菜单


class SystemRoleEditIn(SystemRoleAddIn):
    """编辑角色参数"""
    id: int = Field(gt=0)  # 主键


class SystemRoleOut(SQLModel):
    """系统角色返回信息"""
    id: int  # 主键
    name: str  # 角色名称
    remark: str  # 角色备注
    sort: int  # 角色排序
    is_disable: int  # 是否禁用: [0=否, 1=是]
    create_time: datetime  # 创建时间
    update_time: datetime  # 更新时间


class SystemRoleDetailOut(SystemRoleOut):
    """系统角色返回信息"""
    remark: str  # 角色备注
    menus: List[int] = Field(default_factory=list)  # 关联菜单
    member: int = Field(default=0)  # 成员数量
    sort: int  # 角色排序
    is_disable: int  # 是否禁用: [0=否, 1=是]


class SystemRoleListIn(PageParams):
    name: Union[str, None] = Field(default=None)
    code: Union[str, None] = Field(default=None)
    isStop: Union[int, None, EmptyStrToNone] = Field(default=None)
